package com.metaverse.backend.web;

import com.metaverse.backend.domain.UserToken;
import com.metaverse.backend.exception.BusinessException;
import com.metaverse.backend.security.JwtTokenUtil;
import com.metaverse.backend.security.JwtUser;
import com.metaverse.backend.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


public abstract class BaseController {

    protected Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 获取当前用户
     * @return
     */
    protected JwtUser currentUser(){
        final JwtUser user = SecurityUtils.getAuthenticatedUser();
        if(user == null){
            throw new BusinessException("获取当前用户信息失败");
        }
        return user;
    }


    /**
     * 获取当前roomId
     * @return
     */
    protected  String currentRoomId(){
        JwtUser user = currentUser();
        String username = user.getUsername();
        UserToken userToken = jwtTokenUtil.getUserToken(username);
        return userToken.getRoomId();
    }


}
